<template>
  <el-dialog :title="dialogProps.title" :visible.sync="dialogProps.visible" width="800px" @open="handleDialogOpen()"
    v-loading="loading">
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-form :model="questionnaireModel" :rules="dialogProps.action === 'add' ? addQuestionnaire : editQuestionnaire"
      ref="questionnaireForm" label-width="80px" label-position="right" size="small">
      <el-tabs type="border-card" v-model="activeTabName">
        <el-tab-pane name="questionnaireInfoTab" label="基本信息" class="questionnaire-info-tab-pane">
          <el-row>
            <el-col :span="12">
              <el-form-item label="问卷名称" prop="questionnaireName">
                <el-input ref="questionnaireName" :maxlength="50" v-model="questionnaireModel.questionnaireName"
                  autofocus></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所属分类" prop="category">
                <el-select class="parent-select" v-model="questionnaireModel.category" placeholder="请选择">
                  <el-option v-for="item in optionCT" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="禁用建议" prop="whether">
                <el-switch v-model="questionnaireModel.whether" active-color="#ff4949" inactive-color="#13ce66"
                  :active-value="1" :inactive-value="0"></el-switch>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="性别限制" prop="sexRestriction">
                <el-select class="parent-select" v-model="questionnaireModel.sexRestriction" placeholder="请选择">
                  <el-option v-for="item in optionSR" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
      <el-button type="primary" size="small" @click="submitForm('questionnaireForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { saveQuestionnaire, updateQuestionnaire } from '@/api/Lixueseng520/questionnaire'

  export default {
    name: 'questionnaire-edit',
    data() {
      return {
        loading: true,
        optionSR: [
          { value: '不限', label: '不限' },
          { value: '男', label: '男' },
          { value: '女', label: '女' }
        ],
        optionCT: [
          { value: '评估问卷', label: '评估问卷' },
        ],
        questionnaireModel: {
          id: 0,
          questionnaireName: '',
          category: '',
          whether: '',
          sexRestriction: '',
        },
        dialogProps: {
          visible: false,
          action: '',
          title: ''
        },
        activeTabName: 'questionnaireInfoTab',
        addQuestionnaire: {
          questionnaireName: [
            { required: true, message: '请输入问卷名称', trigger: 'blur' },
            { min: 3, max: 32, message: '长度在 3 到 32 个字符', trigger: 'blur' }
          ],
          category: [
            { required: true, message: '请选择', trigger: 'change' }
          ],
          sexRestriction: [
            { required: true, message: '请选择', trigger: 'change' }
          ]
        },
        editQuestionnaire: {
          questionnaireName: [
            { required: true, message: '请输入问卷名称', trigger: 'blur' },
            { min: 3, max: 32, message: '长度在 3 到 32 个字符', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            if (this.dialogProps.action === 'add') {
              this.doSaveQuestionnaire()
            } else {
              this.doUpdateQuestionnaire()
            }
          } else {
            return false
          }
        })
      },
      doUpdateQuestionnaire() {
        this.loading = true
        updateQuestionnaire(this.questionnaireModel.id, this.questionnaireModel)
          .then(response => {
            this.$message({
              type: 'success',
              message: '修改成功'
            })
            this.loading = false
            this.dialogProps.visible = false
            this.$emit('save-finished')
          })
          .catch(error => {
            this.loading = false
            this.outputError(error)
          })
      },
      doSaveQuestionnaire() {
        this.loading = true
        saveQuestionnaire(this.questionnaireModel)
          .then(response => {
            this.$message({
              type: 'success',
              message: '添加成功'
            })
            this.loading = false
            this.dialogProps.visible = false
            this.$emit('save-finished')
          })
          .catch(error => {
            this.outputError(error)
          })
      },

      handleDialogClose() {
        this.dialogProps.visible = false
      },
      handleDialogOpen() {
        this.$nextTick(() => {
          this.$refs['questionnaireForm'].clearValidate()
          this.$refs['questionnaireName'].focus()
        })
      },
      outputError(error) {
        console.log(error.response ? error.response : error.message)
        this.loading = false
        this.$message({
          showClose: true,
          message: '出错了，请按F12查看浏览器日志。',
          type: 'error'
        })
      }
    },
    mounted: function () {
      this.$nextTick(() => {
        this.$on('openEditUserDialog', function (questionnaire) {
          this.questionnaireModel = questionnaire
          this.dialogProps.action = 'edit'
          this.dialogProps.title = '编辑'
          this.activeTabName = 'questionnaireInfoTab'
          this.dialogProps.visible = true
        })
        this.$on('openAddUserDialog', function () {
          this.questionnaireModel = {
            roleIds: []
          }
          this.dialogProps.action = 'add'
          this.dialogProps.title = '新增'
          this.activeTabName = 'questionnaireInfoTab'
          this.dialogProps.visible = true
        })
      })
    }
  }
</script>

<style lang="scss" scoped>
  .questionnaire-info-tab-pane {
    margin-top: 10px;
  }

  .parent-select {
    width: 230px;
  }

  .properties-textarea {
    width: 496px;
  }
</style>
